namespace Localisation.WebDemo
{
    using System;
    using System.Globalization;
    using System.Threading;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public partial class _Default : Page
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            foreach (ListItem item in UserCulture.Items)
            {
                item.Selected = (item.Value == Thread.CurrentThread.CurrentCulture.Name);
            }
            SelectedCulture.Text = Thread.CurrentThread.CurrentCulture.DisplayName;
        }

        /// <summary>
        /// Sets the <see cref="P:System.Web.UI.Page.Culture"></see> and <see cref="P:System.Web.UI.Page.UICulture"></see> for the current thread of the page.
        /// </summary>
        protected override void InitializeCulture()
        {
            string selectedCulture = Request["UserCulture"];
            Response.Cookies["UserCulture"].Value = selectedCulture;
            Response.Cookies["UserCulture"].Expires = DateTime.Now.AddDays(30);

            if (!string.IsNullOrEmpty(selectedCulture))
            {
                UICulture = selectedCulture;
                Culture = selectedCulture;

                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(selectedCulture);
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(selectedCulture);
            }
            base.InitializeCulture();
        }
    }
}
